



axios.defaults.baseURL = 'http://localhost:3000/';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';



// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    const authorization = response.headers.authorization
    console.log(response.headers);
    authorization && localStorage.setItem("token", authorization)


    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
});



document.querySelector("#login-btn").addEventListener("click", () => {

    const username = document.querySelector("#username").value;
    const password = document.querySelector("#password").value;
    const email = document.querySelector("#email").value;


    axios.post('/api/login', {
        username,
        password,
        email
    }).then(res => {
        console.log(res);
        console.log(res.data);
        debugger
        if (res.data.status === 1) {
            alert('登录成功')
            location.href = "/index"
        }
        if (res.data.status === 0) {
            alert("登录失败")
        }
    }).catch(err => {

        //  console.log(err);
    })



})



document.querySelector("#register-btn").addEventListener("click", () => {

    const username = document.querySelector("#username").value;
    const password = document.querySelector("#password").value;
    const email = document.querySelector("#email").value;


    axios.post('/api/register', {
        username,
        password,
        email
    }).then(res => {
        console.log(res.data);
    }).catch(err => {
        console.log(err);
    })



})
